package com.cn.yds.bdy.controller;

import com.cn.yds.bdy.dao.model.Topic;
import com.cn.yds.bdy.dao.model.TopicModel;
import com.cn.yds.bdy.dao.model.TopicPo;
import com.cn.yds.bdy.service.OptService;
import com.cn.yds.bdy.service.TopicService;
import com.cn.yds.bdy.service.UserService;
import com.cn.yds.bdy.vo.ChooseTopicResultVo;
import com.cn.yds.bdy.vo.ResponseResult;
import com.cn.yds.bdy.vo.SearchParamVo;
import com.cn.yds.bdy.vo.StudentChoiceVo;
import com.cn.yds.bdy.vo.StudentVo;
import com.cn.yds.bdy.vo.TeacherChoiceVo;
import com.cn.yds.bdy.vo.TopicCollectVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description:
 *
 * @ClassName: TopicController
 * @Author: Jameson Yang
 * @Date: created in 15:25 01/10/19
 */
@Slf4j
@RestController
@RequestMapping("topic")
@Api("Topic operation.")
public class TopicController {

    @Autowired
    private TopicService topicService;
    @Autowired
    private OptService optService;
    @Autowired
    private UserService userService;


    /**
     * Get all topic to choice for student
     *
     * @return List<TopicModel>
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResponseResult getAllTopicsToChoice(@RequestBody SearchParamVo searchParam) {

        Assert.isTrue((1 == searchParam.getRole() || 0 == searchParam.getRole()), "Role must be student or teacher.");
        Assert.notNull(searchParam.getUserId(), "User id is not empty.");
        log.info("param: {}", searchParam);
        Page<Topic> topics;
        if (Objects.nonNull(searchParam.getType()) && 1 == searchParam.getType()) {
            log.info("topic manager for student");
            topics = optService.getSelectedTopics(searchParam);
        } else {
            log.info("topic manager for teacher and selecting topic for student");
            topics = topicService.findTopics(searchParam);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("list", topics.getContent());
        map.put("total", topics.getTotalElements());
        return ResponseResult.ok(map);
    }

    /**
     * Get topic by topicId for teacher
     *
     * @param topicId topicId
     * @return TopicModel
     */
    @RequestMapping(value = "getTopicDetail", method = RequestMethod.POST)
    public ResponseResult getTopicDetail(@RequestBody Integer topicId) {

        TopicPo topicPo = topicService.findById(topicId);
        List<StudentVo> students = topicService.getStudentsInOneTopic(topicId);
        Map<String, Object> result = new HashMap<>();
        result.put("topic", topicPo);
        result.put("students", students);
        return ResponseResult.ok(result);
    }

    /**
     * Update topic for teacher
     *
     * @param topicModel topicModel
     * @return ResponseResult
     */
    @RequestMapping(value = "saveAndUpdateTopic", method = RequestMethod.POST)
    public ResponseResult saveAndUpdateTopic(@RequestBody TopicModel topicModel) {
        topicService.updateTopic(topicModel);
        return ResponseResult.ok();
    }

    /**
     * Teacher choice student for the topic
     *
     * @param teacherChoiceVo teacherChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "choiceStudentForTopic", method = RequestMethod.POST)
    public ResponseResult choiceStudentForTopic(@RequestBody TeacherChoiceVo teacherChoiceVo) {
        Integer result = optService. teacherChoiceStudents(teacherChoiceVo);
        if (result == 0) {
            return ResponseResult.ok("选择失败！该学生已经被其他老师提前选择了.请刷新页面.");
        }

        return ResponseResult.ok("选择成功！");
    }

    /**
     * Teacher update students for the topic
     *
     * @param teacherChoiceVo teacherChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "updateStudentForTopic", method = RequestMethod.POST)
    public ResponseResult updateStudentForTopic(@RequestBody TeacherChoiceVo teacherChoiceVo) {
        optService.teacherUpdateStudentsForTopic(teacherChoiceVo);
        return ResponseResult.ok();
    }

    /**
     * Student choice topics
     *
     * @param studentChoiceVo studentChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "studentChoiceTopic", method = RequestMethod.POST)
    public ResponseResult studentChoiceTopic(@RequestBody StudentChoiceVo studentChoiceVo) {
        Map<String, Object> map = new HashMap<>();
        Assert.notNull(studentChoiceVo.getStudentId(), "Student id can't empty.");
        if (!CollectionUtils.isEmpty(studentChoiceVo.getTopicIds())
                && studentChoiceVo.getTopicIds().size() > 3) {
            map.put("result", 201);
        }

        Integer auth = optService.validateChooseAuth(studentChoiceVo.getStudentId());
        map.put("result", 202);
        map.put("info", auth);
        if (auth == 1) {
            ChooseTopicResultVo resultVo = optService.studentChoiceTopic(studentChoiceVo);
            map.put("result", 203);
            map.put("info", resultVo);
        }
        return ResponseResult.ok(map);
    }

    /**
     * Get Student choose topics authorize
     *
     * @param studentChoiceVo studentChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "chooseTopicAuth", method = RequestMethod.POST)
    public ResponseResult getChooseTopicAuth(@RequestBody StudentChoiceVo studentChoiceVo) {
        Assert.notNull(studentChoiceVo.getStudentId(), "Student id can't empty.");
        Integer auth = optService.validateChooseAuth(studentChoiceVo.getStudentId());
        return ResponseResult.ok(auth);
    }


    /**
     * Student delete the underway topic
     *
     * @param studentChoiceVo studentChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "studentDeleteTopic", method = RequestMethod.POST)
    public ResponseResult studentDeleteTopic(@RequestBody StudentChoiceVo studentChoiceVo) {
        Integer deleteStatus = optService.studentDeleteTopic(studentChoiceVo);
        return ResponseResult.ok(deleteStatus);
    }


    /**
     * Census display for teacher
     *
     * @param teacherChoiceVo teacherChoiceVo
     * @return ResponseResult
     */
    @RequestMapping(value = "topicCensus", method = RequestMethod.POST)
    public ResponseResult topicCensus(@RequestBody TeacherChoiceVo teacherChoiceVo) {
        Assert.notNull(teacherChoiceVo.getTeacherId(),"teacher id can't is empty.");
        TopicCollectVo topicCollectVo = topicService.collectTeachersTopic(teacherChoiceVo.getTeacherId());
        return ResponseResult.ok(topicCollectVo);
    }


}
